<?php
class TopJoinController  extends Controller {
 
	public function __construct($data) {
        parent::__construct($data);
    }  
	
     public function index(){
     	//ヘッド、メニューなどsmarty設定
     	require_once( '../system/ecjp_m.php' );
     	$this->_load_php_file("chklogin.php");
        if(!isset($_SESSION['USER'])){	    	
	    require_once( '../system/cookie.php' );  
	    }
     	$this->_load_php_file("Class_FUNC.php");
     		$site = Class_FUNC::get_site();
	    if(isset($site['site_keyword']))
	    $this->assign('site_keyword', $site['site_keyword']);
        if(isset($site['site_description']))
	    $this->assign('site_description', $site['site_description']);
	    if(isset($site['site_title']))
	    $this->assign('site_title', 'メンバー登録  | '.$site['site_title']);
     	
        $this->assign('sex_radios', $ecjp_user_sex);
     	$this->assign('send_radios', $ecjp_user_send);
     	$this->assign('year4', Class_FUNC::get_years());
        $this->assign('moon4', Class_FUNC::get_moons());
     	
     	$this->_load_Class_DAO_OTHER();
		$dao_local = Class_DAO_OTHER::get_local_array('');
		
		if (isset($dao_local)){
		 	$this->assign('dao_local', $dao_local);
		}

		$this->_load_mdao_class("t_m_goods");
	    $goods_dao = new t_m_goods;
	    
	    $this->_load_mdao_class("t_m_news");
	    $news_dao = new t_m_news;
		
		  //売上ランキング
	       $sql = "SELECT b.id,b.goods_name,b.goods_image,b.goods_unitprice FROM d_good_rank a inner JOIN d_goods b ON a.good_id = b.id ORDER BY a.rank";
	       $charts_data = $goods_dao->get_rows($sql);
	        foreach ($charts_data as &$value){
	          if(isset($value['goods_image']) && !empty($value['goods_image'])){
	     	     $value['goods_image'] = Class_FUNC::get_imagepath($value['id'],$value['goods_image'],101);
	       }
	         if(isset($value['goods_unitprice']) && !empty($value['goods_unitprice'])){
	          	 $value['goods_rate'] = $value['goods_unitprice']+$value['goods_unitprice'] * RATE;
	          }
	     }     
	       if (isset($charts_data))
	       $this->assign('charts_data',$charts_data);
	     
	     //What's New
           $sql = 'SELECT * FROM  m_news where site_id = 1 and  st_time<current_timestamp and ed_time>current_timestamp order by date desc limit 0,10';
           $newslist_data = $news_dao->get_rows($sql);
           if (isset($newslist_data))
           $this->assign('news_datalist', $newslist_data);
	     
	     //商品category
	       $sql = 'SELECT * FROM  m_categorygood';
           $category_data = $goods_dao->get_rows($sql);
           if (isset($category_data))
           $this->assign('category_data', $category_data);
		
		$this->assign('sp_meta', $this->fetch('sp/sp_meta.html'));
		$this->assign('sp_head', $this->fetch('sp/sp_head.html'));
		$this->assign('sp_foot', $this->fetch('sp/sp_foot.html'));
		$this->assign('categorying', $this->fetch('sp/categorying.html'));
         $this->assign('newsing', $this->fetch('sp/news.html'));
	     $this->assign('chartsing', $this->fetch('sp/charts.html'));
     	$this->display('sp/join_member.html');
     }
      
     public function chkregist(){
     	//ヘッド、メニューなどsmarty設定
     	require_once( '../system/ecjp_m.php' );
     	$this->_load_php_file("chklogin.php");
        $this->_load_php_file("Class_FUNC.php");
        $this->_load_mdao_class ( "t_m_user" );
	    $db = new t_m_user;
	      $site = Class_FUNC::get_site();
	          if(isset($site['site_keyword']))
	          $this->assign('site_keyword', $site['site_keyword']);
             if(isset($site['site_description']))
	         $this->assign('site_description', $site['site_description']);
	         if(isset($site['site_title']))
	         $this->assign('site_title', '確認画面 | メンバー登録 |  '.$site['site_title']);
        
        $this->assign('sex_radios', $ecjp_user_sex);
     	$this->assign('send_radios', $ecjp_user_send);
     	$this->assign('year4', Class_FUNC::get_years());
        $this->assign('moon4', Class_FUNC::get_moons());
        $this->_load_Class_DAO_OTHER();
		$dao_local = Class_DAO_OTHER::get_local_array('');
		
		if (isset($dao_local)){
		 	$this->assign('dao_local', $dao_local);
		}
        
     	require_once( '../system/Class_ERROR.php' );
     	
     	$err = new Class_ERROR ();
     	$forms = $this->p;
     	$chkcols  = array(
			"email1"     => array(CHK_EXIST ,CHK_EMAIL),
     	    "email2"     => array(CHK_EXIST ,CHK_EMAIL),
     	    "login_id"   => array(CHK_EXIST ,CHK_ALNUM),
     	    "login_pwd"  => array(CHK_EXIST ,CHK_ALNUM),
     	    "user_name1"  => array(CHK_EXIST),
     	    "user_name2"   => array(CHK_EXIST),
     	    "user_name_furikana1"  => array(CHK_EXIST),
     	    "user_name_furikana2"   => array(CHK_EXIST),
     	    "zipcode1"      => array(CHK_EXIST),
     	    "zipcode2"      => array(CHK_EXIST),
     	    "local_cd"      => array(CHK_EXIST),
     	    "treet_name1"   => array(CHK_EXIST),
     	    "treet_name2"   => array(CHK_EXIST),
     	    "address"       => array(CHK_EXIST),
     	    "tel_number1"   => array(CHK_EXIST),
     	    "tel_number2"   => array(CHK_EXIST),
     	    "tel_number3"    => array(CHK_EXIST),
     	    "birth1"         => array(CHK_EXIST),
     	    "birth2"         => array(CHK_EXIST)
			 );
         foreach( $chkcols as $kcol => $chkval ) {
				$err_mes [$kcol] = $err->check ( $forms [$kcol], $chkval );
         }
         //
            if(!isset($err_mes ['email2'])){
            	if($forms['email1'] != $forms['email2']){
            		$err_mes ['email2']='<p style="display: block;"  class="error">確認のため、同じメールアドレスを再度入力してください</p>';
            		$err->clear = false;
            	}
            }
            
            if(!isset($err_mes ['login_id'])){
            	 $len = mb_strlen($forms['login_id'],"UTF8");
            	 if($len<6){
            	 	$err_mes ['login_id']='<p style="display: block;"  class="error">6文字以上入力してください</p>';
            	 	$err->clear = false;
            	 } else if($forms['login_id'] == $forms['email1']){
            	 	$err_mes ['login_id']='<p style="display: block;"  class="error">メールアドレスと同じでも構いません</p>';
            	 	$err->clear = false;
            	 } else {
            	 	   $where = array();
            	 	   $where[] = sprintf(" (a.login_id= '%s' )", $forms['login_id']);
            	 	   $data = $db->search($where);
            	 	  if(!empty($data)){
            	 	  	$err_mes ['login_id']='<p style="display: block;"  class="error">入力されたIDは既に存在しています</p>';
            	 	    $err->clear = false;
            	 	  }      	 	  
            	 }
            }
            
            if(!isset($err_mes ['login_pwd'])){
            	$len = mb_strlen($forms['login_pwd'],"UTF8");
            	 if($len<6 || $len>10 ){
            	 	$err_mes ['login_pwd']='<p style="display: block;"  class="error">6～10文字入力してください</p>';
            	 	$err->clear = false;
            	 }
            }
            
            if( !isset($forms['sex']) || empty($forms['sex'])) {
              $err_mes ['sex']= '<p style="display: block;"  class="error">必須項目です</p>';
              $err->clear = false;
            }
     
         if($err->clear){            
         	 if (isset ( $forms ))		
		     $this->assign ( 'forms', $forms );
		     if(isset($forms['local_cd']) && !empty($forms['local_cd'])) 
		     $this->assign ( 'local_name', $dao_local[$forms['local_cd']]);
		     if(isset($forms['sent_local_cd']) && !empty($forms['sent_local_cd'])) 
		     $this->assign ( 'sent_local_name', $dao_local[$forms['sent_local_cd']]);
		     if(isset($forms['sex']) && !empty($forms['sex']))
		     $this->assign ( 'sex', $ecjp_user_sex[$forms['sex']]);
		     if(isset($forms['send']) && !empty($forms['send']))
		     $this->assign ( 'send', $ecjp_user_send[$forms['send']]);
		     
		     
		     	$this->_load_mdao_class("t_m_goods");
	            $goods_dao = new t_m_goods;
	    
	            $this->_load_mdao_class("t_m_news");
	            $news_dao = new t_m_news;
	            
	            $this->_load_php_file("Class_FUNC.php");
         	
         	  //売上ランキング
	       $sql = "SELECT b.id,b.goods_name,b.goods_image,b.goods_unitprice FROM d_good_rank a inner JOIN d_goods b ON a.good_id = b.id ORDER BY a.rank";
	       $charts_data = $goods_dao->get_rows($sql);
	        foreach ($charts_data as &$value){
	          if(isset($value['goods_image']) && !empty($value['goods_image'])){
	     	     $value['goods_image'] = Class_FUNC::get_imagepath($value['id'],$value['goods_image'],101);
	       }
	         if(isset($value['goods_unitprice']) && !empty($value['goods_unitprice'])){
	          	 $value['goods_rate'] = $value['goods_unitprice']+$value['goods_unitprice'] * RATE;
	          }
	     }
	       if (isset($charts_data))
	       $this->assign('charts_data',$charts_data);
	     
	     //What's New
           $sql = 'SELECT * FROM  m_news where site_id = 1 and  st_time<current_timestamp and ed_time>current_timestamp order by date desc limit 0,10';
           $newslist_data = $news_dao->get_rows($sql);
           if (isset($newslist_data))
           $this->assign('news_datalist', $newslist_data);
	     
	     //商品category
	       $sql = 'SELECT * FROM  m_categorygood';
           $category_data = $goods_dao->get_rows($sql);
           if (isset($category_data))
           $this->assign('category_data', $category_data);
		     
             $this->assign('sp_meta', $this->fetch('sp/sp_meta.html'));
		     $this->assign('sp_head', $this->fetch('sp/sp_head.html'));
		     $this->assign('sp_foot', $this->fetch('sp/sp_foot.html'));
		     $this->assign('categorying', $this->fetch('sp/categorying.html'));
             $this->assign('newsing', $this->fetch('sp/news.html'));
	         $this->assign('chartsing', $this->fetch('sp/charts.html'));
         	 $this->display('sp/join_member_check.html');
         	 exit;
         }else {
         	
         		$this->_load_mdao_class("t_m_goods");
	            $goods_dao = new t_m_goods;
	    
	            $this->_load_mdao_class("t_m_news");
	            $news_dao = new t_m_news;
	            
	            $this->_load_php_file("Class_FUNC.php");
         	
         	  //売上ランキング
	       $sql = "SELECT b.id,b.goods_name,b.goods_image,b.goods_unitprice FROM d_good_rank a inner JOIN d_goods b ON a.good_id = b.id ORDER BY a.rank";
	       $charts_data = $goods_dao->get_rows($sql);
	        foreach ($charts_data as &$value){
	          if(isset($value['goods_image']) && !empty($value['goods_image'])){
	     	     $value['goods_image'] = Class_FUNC::get_imagepath($value['id'],$value['goods_image'],101);
	       }
	         if(isset($value['goods_unitprice']) && !empty($value['goods_unitprice'])){
	          	 $value['goods_rate'] = $value['goods_unitprice']+$value['goods_unitprice'] * RATE;
	          }
	     }
	       if (isset($charts_data))
	       $this->assign('charts_data',$charts_data);
	     
	     //What's New
           $sql = 'SELECT * FROM  m_news where site_id = 1 and  st_time<current_timestamp and ed_time>current_timestamp order by date desc limit 0,10';
           $newslist_data = $news_dao->get_rows($sql);
           if (isset($newslist_data))
           $this->assign('news_datalist', $newslist_data);
	     
	     //商品category
	       $sql = 'SELECT * FROM  m_categorygood';
           $category_data = $goods_dao->get_rows($sql);
           if (isset($category_data))
           $this->assign('category_data', $category_data);
         	
         	
         	 if (isset ( $forms ))		
		     $this->assign ( 'forms', $forms );
		     $this->assign ( 'err_mes', $err_mes );  
		      $site = Class_FUNC::get_site();
	          if(isset($site['site_keyword']))
	          $this->assign('site_keyword', $site['site_keyword']);
             if(isset($site['site_description']))
	         $this->assign('site_description', $site['site_description']);
	         if(isset($site['site_title']))
	         $this->assign('site_title', 'メンバー登録  | '.$site['site_title']);  
             
	         $this->assign('sp_meta', $this->fetch('sp/sp_meta.html'));
		     $this->assign('sp_head', $this->fetch('sp/sp_head.html'));
		     $this->assign('sp_foot', $this->fetch('sp/sp_foot.html'));
		     $this->assign('categorying', $this->fetch('sp/categorying.html'));
             $this->assign('newsing', $this->fetch('sp/news.html'));
	         $this->assign('chartsing', $this->fetch('sp/charts.html'));
     	     $this->display('sp/join_member.html');
         }
       }
       
       public function insert(){
       	    //ヘッド、メニューなどsmarty設定
     	   require_once( '../system/ecjp_m.php' );
     	   $this->_load_php_file("chklogin.php");
       	     $this->_load_php_file("Class_FUNC.php");
     	     $site = Class_FUNC::get_site();
	          if(isset($site['site_keyword']))
	          $this->assign('site_keyword', $site['site_keyword']);
             if(isset($site['site_description']))
	         $this->assign('site_description', $site['site_description']);
	         if(isset($site['site_title']))
	         $this->assign('site_title', '確認画面 | メンバー登録 |  '.$site['site_title']);
     	   
       	   $this->_load_mdao_class ( "t_m_user" );
		   $dao = new t_m_user;
		   
		   $this->_load_mdao_class ( "t_m_delivery" );
		   $dao_d = new t_m_delivery;
		   
		   $this->_load_mdao_class ( "t_m_mail" );
		   $dao_mail = new t_m_mail;
		   
       	   $this->_load_php_file("Class_FUNC.php");
		   
		   $forms = $this->p;
       	   $values = $this->set_user_insert_values ($forms);
       	   if($dao->add($values)){
       	   	  // $_SESSION['USER']['USER_site_id']	= $forms['site_id'];
			   $_SESSION['USER']['USER_login_id']	= $forms['login_id'];
			   $_SESSION['USER']['USER_name']	= $forms['user_name1'].'  '.$forms['user_name2'];
			   $_SESSION['USER']['USER_local_cd']	=  $forms['local_cd'];
			   
			   
			   if(Class_FUNC::senduserMail('39643768@qq.com','aaa','aaaaaa')){
			        	$v_mail["type"]="会員登録完了";
			        	$v_mail["date"]=date('Y-m-d H:i:s');
			        	$v_mail["name"]="aaaaaa";
			        	$v_mail["address"]="39643768@qq.com";
			        	$v_mail["return"]="送信OK";
			        	//$dao_mail->add($v_mail);
			   }else {
			   	        $v_mail["type"]="会員登録完了";
			        	$v_mail["date"]=date('Y-m-d H:i:s');
			        	$v_mail["name"]="aaaaaa";
			        	$v_mail["address"]="39643768@qq.com";
			        	$v_mail["return"]="送信失敗";
			        	//$dao_mail->add($v_mail);
			   	  
			   }
			   	   
			   			   
			   $values = $this->set_dv_insert_values ($forms);
			   $values['login_id'] = $forms['login_id'];
			   $dao_d->add($values);

			   
			  $this->_load_mdao_class("t_m_goods");
	            $goods_dao = new t_m_goods;
	    
	            $this->_load_mdao_class("t_m_news");
	            $news_dao = new t_m_news;
         	
         	  //売上ランキング
	       $sql = "SELECT b.id,b.goods_name,b.goods_image,b.goods_unitprice FROM d_good_rank a inner JOIN d_goods b ON a.good_id = b.id ORDER BY a.rank";
	       $charts_data = $goods_dao->get_rows($sql);
	        foreach ($charts_data as &$value){
	          if(isset($value['goods_image']) && !empty($value['goods_image'])){
	     	     $value['goods_image'] = Class_FUNC::get_imagepath($value['id'],$value['goods_image'],101);
	       }
	         if(isset($value['goods_unitprice']) && !empty($value['goods_unitprice'])){
	          	 $value['goods_rate'] = $value['goods_unitprice']+$value['goods_unitprice'] * RATE;
	          }
	     }
	       if (isset($charts_data))
	       $this->assign('charts_data',$charts_data);
	     
	     //What's New
           $sql = 'SELECT * FROM  m_news where site_id = 1 and  st_time<current_timestamp and ed_time>current_timestamp order by date desc limit 0,10';
           $newslist_data = $news_dao->get_rows($sql);
           if (isset($newslist_data))
           $this->assign('news_datalist', $newslist_data);
	     
	     //商品category
	       $sql = 'SELECT * FROM  m_categorygood';
           $category_data = $goods_dao->get_rows($sql);
           if (isset($category_data))
           $this->assign('category_data', $category_data); 
			   
			 $this->assign('sp_meta', $this->fetch('sp/sp_meta.html'));
			 $this->assign('sp_head', $this->fetch('sp/sp_head.html')); 
			 $this->assign('categorying', $this->fetch('sp/categorying.html'));
             $this->assign('newsing', $this->fetch('sp/news.html'));
	         $this->assign('chartsing', $this->fetch('sp/charts.html')); 
			 $this->assign('sp_foot', $this->fetch('sp/sp_foot.html'));
       	   	 $this->display('sp/join_member_done.html');
       	   }
       	  
       }
        protected function set_dv_insert_values($forms) {
        	$values = array();
        	$values ["sent_name1"] = $forms ['sent_name1'];	
        	$values ["sent_name2"] = $forms ['sent_name2'];	
        	$values ["sent_name_furikana1"] = $forms ['sent_name_furikana1'];	
        	$values ["sent_name_furikana2"] = $forms ['sent_name_furikana2'];	
        	$values ["sent_zipcode"] = $forms ['sent_zipcode'];	
        	$values ["sent_local_cd"] = $forms ['sent_local_cd'];	
        	$values ["sent_treet_name"] = $forms ['sent_treet_name'];	
        	$values ["sent_address"] = $forms ['sent_address'];	
        	$values ["sent_tel_number"] = $forms ['sent_tel_number'];	
        	$values ["sent_workshop_name1"] = $forms ['sent_workshop_name1'];	
        	$values ["sent_workshop_name2"] = $forms ['sent_workshop_name2'];	
        	return $values;
        }
       
         protected function set_user_insert_values($forms) {
			$values = array ();		
			$values ["login_id"] = $forms ['login_id'];	
		    $values ["email"] = $forms ['email'];
		    $values ["login_pwd"] = $forms ['login_pwd'];
		    $values ["user_name1"] = $forms ['user_name1'];
		    $values ["user_name2"] = $forms ['user_name2'];
		    $values ["user_name_furikana1"] = $forms ['user_name_furikana1'];
		    $values ["user_name_furikana2"] = $forms ['user_name_furikana2'];
		    $values ["zipcode"] = $forms ['zipcode'];
		    $values ["local_cd"] = $forms ['local_cd'];
		    $values ["treet_name1"] = $forms ['treet_name1'];
		    $values ["treet_name2"] = $forms ['treet_name2'];
		    $values ["address"] = $forms ['address'];
		    $values ["tel_number"] = $forms ['tel_number'];
		    $values ["mobile_number"] = $forms ['mobile_number'];
		    $values ["workshop_name"] = $forms ['workshop_name'];
		    $values ["workshop_name_furikana"] = $forms ['workshop_name_furikana'];
		    $values ["birth"] = $forms ['birth'];
		    $values ["sex"] = $forms ['sex'];
		    $values ["send"] = $forms ['send'];
		    return $values;
	} 
       
     }
    
?>
